{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>大纲<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#fab实现1\" data-toc-modified-id=\"fab实现1-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>fab实现1</a></span></li><li><span><a href=\"#fab实现2\" data-toc-modified-id=\"fab实现2-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>fab实现2</a></span></li><li><span><a href=\"#fab实现3\" data-toc-modified-id=\"fab实现3-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>fab实现3</a></span></li><li><span><a href=\"#fab实现4\" data-toc-modified-id=\"fab实现4-4\"><span class=\"toc-item-num\">4&nbsp;&nbsp;</span>fab实现4</a></span></li><li><span><a href=\"#yield理解\" data-toc-modified-id=\"yield理解-5\"><span class=\"toc-item-num\">5&nbsp;&nbsp;</span>yield理解</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过斐波拉契数列的简单实现来理解，yield"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# fab实现1\n",
    "方法：使用条件判断的方式输出结果\n",
    "\n",
    "缺点：无法保存结果到对象中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T09:23:39.824408Z",
     "start_time": "2018-05-07T09:23:39.818318Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "def fab(max):\n",
    "    n = 0\n",
    "    a = 0\n",
    "    b = 1\n",
    "    while n < max:\n",
    "        print(b)\n",
    "        a, b =b, a + b\n",
    "        n = n + 1\n",
    "\n",
    "fab(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "直接打印结果，复用性差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# fab实现2\n",
    "方法：新建一个对象，保存生成的数值\n",
    "\n",
    "缺点：max变大，内存占用会很多"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-02T09:06:33.861783Z",
     "start_time": "2018-05-02T09:06:33.855481Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 2, 3, 5]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def fab(max):\n",
    "    n = 0\n",
    "    a = 0\n",
    "    b = 1\n",
    "    res = []\n",
    "    while n < max:\n",
    "        res.append(b)\n",
    "        a, b =b, a + b\n",
    "        n = n + 1\n",
    "        \n",
    "    return res\n",
    "\n",
    "fab(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# fab实现3\n",
    "使用类实现，功能能够实现，就是程序显得复杂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-07T09:20:01.983068Z",
     "start_time": "2018-05-07T09:20:01.976628Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "class Fab(object):\n",
    "    def __init__(self,max):\n",
    "        self.max = max\n",
    "        self.n = 0\n",
    "        self.a = 0\n",
    "        self.b = 1\n",
    "    \n",
    "    def __iter__(self):\n",
    "        return self\n",
    "    \n",
    "    def __next__(self):\n",
    "        if self.n < self.max:\n",
    "            r = self.b\n",
    "            self.a, self.b = self.b, self.a + self.b\n",
    "            self.n = self.n + 1\n",
    "            return r\n",
    "        # 是抛出异常，而不是返回异常\n",
    "        raise StopIteration()\n",
    "\n",
    "for i in Fab(5):\n",
    "    print(i)        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-02T09:20:48.336455Z",
     "start_time": "2018-05-02T09:20:06.433Z"
    }
   },
   "source": [
    "这个程序会不断打印空信息，是个bug，不知谁的锅"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# fab实现4\n",
    "使用yield实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-26T15:15:13.417764Z",
     "start_time": "2018-05-26T15:15:13.411232Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "13\n",
      "21\n",
      "34\n",
      "55\n",
      "89\n",
      "144\n",
      "233\n",
      "377\n",
      "610\n"
     ]
    }
   ],
   "source": [
    "def fab(max):\n",
    "    n = 0\n",
    "    a = 0\n",
    "    b = 1\n",
    "    while n < max:\n",
    "        yield b\n",
    "        a, b = b, a + b\n",
    "        n = n + 1\n",
    "\n",
    "for i in fab(15):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# yield理解\n",
    "\n",
    "yield相当于告诉Python解释器：我要在这个地方中断一下，你先给我保存现场，我把值返回给调用者，等我回来时继续从这个环境执行程序\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda all lib",
   "language": "python",
   "name": "base"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "大纲",
   "title_sidebar": "目录",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
